package com.jerry.walking.module.activityTravel.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jerry.walking.R;
import com.jerry.walking.base.BaseActivity;
import com.jerry.walking.base.BaseApplication;
import com.jerry.walking.base.BaseViewHolder;
import com.jerry.walking.base.MyBaseAdapter;
import com.jerry.walking.constant.Urls;
import com.jerry.walking.module.activityTravel.bean.TravelRecommendFirst;
import com.jerry.walking.utils.OkHttpUtils;
import com.jerry.walking.views.ExpandListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Yang on 2016/5/11.
 *
 *          游记 ---> 推荐 ---> 点击显示界面
 *
 *
 */
public class FirstActivity extends BaseActivity {

    //后退键
    @BindView(R.id.iv_travel_recommend_first)
    ImageView imageView1;

    @BindView(R.id.iv_travel_recommend_first1)
    ImageView imageView2;
    @BindView(R.id.iv_travel_recommend_first2)
    ImageView imageView3;

    @BindView(R.id.tv_travel_recommend_first)
    TextView textView1;
    @BindView(R.id.tv_travel_recommend_first1)
    TextView textView2;
    @BindView(R.id.tv_travel_recommend_first2)
    TextView textView3;
    @BindView(R.id.tv_travel_recommend_first3)
    TextView textView4;

    @BindView(R.id.lv_travel_recommend_first)
    ExpandListView listView;

    ImageLoader imageLoader;

    private String id = "";

    TravelRecommendFirst travelRecommendFirst;

    private List<TravelRecommendFirst.LogBean.PostsBean> items;

    private MyBaseAdapter<TravelRecommendFirst.LogBean.PostsBean> adapter;

    @Override
    protected int getLayoutId() {

        //得到id
        id = getIntent().getStringExtra("id");

        return R.layout.activity_travel_recommend_first;
    }

    @Override
    protected void initViews() {
        super.initViews();

        imageLoader = BaseApplication.getApplication().getImageLoader();

        //设置listview子控件为可点击的
        listView.setItemsCanFocus(true);

        travelRecommendFirst = new TravelRecommendFirst();
        items = new ArrayList<>();

        //listview适配器适配
        adapter = new MyBaseAdapter<TravelRecommendFirst.LogBean.PostsBean>(this, items) {
            @Override
            public int getLayoutId() {
                return R.layout.adapter_travel_recommend_first;
            }

            @Override
            public BaseViewHolder getViewHolder() {
                return new ViewHold();
            }

            @Override
            public void bindViews(View view, BaseViewHolder viewHolder) {
                final ViewHold holder = (ViewHold) viewHolder;
                DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .cacheOnDisk(true)
                        .cacheInMemory(true)
                        .imageScaleType(ImageScaleType.EXACTLY)
                        .build();
                imageLoader.displayImage(items.get(holder.position).getPictures().get(0).getPicture(),
                        holder.imageView, options);
//                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(Integer.parseInt(items.get(holder.position).getPictures().get(0).getWidth()), Integer.parseInt(items.get(holder.position).getPictures().get(0).getHeight()));
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-1, -1);
                holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                holder.imageView.setLayoutParams(params);

                holder.textView1.setText(items.get(holder.position).getFav_count());
                holder.textView2.setText(items.get(holder.position).getComment_count());
                holder.textView3.setText(items.get(holder.position).getIs_like()+"");

                holder.textView4.setText(items.get(holder.position).getDescription());

                String day = items.get(holder.position).getDay();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
                String format = simpleDateFormat.format(new Date(Integer.parseInt(day)));
                holder.textView5.setText(format);


                //监听器----------

                //图片
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(FirstActivity.this, BigPicture.class);
                        intent.putExtra("name", travelRecommendFirst.getLog().getCreated_by().getNickname());
                        intent.putExtra("nikeName", travelRecommendFirst.getLog().getCreated_by().getNickname());
                        intent.putExtra("cover", travelRecommendFirst.getLog().getCover());
                        intent.putExtra("description", items.get(holder.position).getDescription());
                        intent.putExtra("picture", items.get(holder.position).getPictures().get(0).getPicture());
                        intent.putExtra("fav_count", items.get(holder.position).getFav_count());
                        intent.putExtra("comment_count", items.get(holder.position).getComment_count());
                        FirstActivity.this.startActivity(intent);
                    }
                });

                //赞
                holder.imageView1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });

                //留言
                holder.imageView2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });

                //举报
                holder.imageView3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });

            }
        };
        listView.setAdapter(adapter);
    }

    @Override
    protected void loadData() {
        super.loadData();

        OkHttpUtils.get(Urls.RECOMMEND_LIST_ITEM + id + Urls.RECOMMEND_LIST_ITEM_CURRENT + id + Urls.RECOMMEND_LIST_ITEM_END, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {
                Gson gson = new Gson();

                travelRecommendFirst = gson.fromJson(data, TravelRecommendFirst.class);

                //解析要传递的数据
                final String avatar = travelRecommendFirst.getLog().getCreated_by().getAvatar();
                final String nickname = travelRecommendFirst.getLog().getCreated_by().getNickname();
                final String name = travelRecommendFirst.getLog().getName();
                final String source = travelRecommendFirst.getLog().getSource();
                final String cover = travelRecommendFirst.getLog().getCover();

                //对所有时间进行修改格式化
                String total_days = travelRecommendFirst.getLog().getTotal_days();//经历几天
                String photo_number = travelRecommendFirst.getLog().getPhoto_number();//图片数
                String created_date = travelRecommendFirst.getLog().getCreated_date();//时间
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
                String format = simpleDateFormat.format(new Date(Long.parseLong(created_date)));

                final String date = format+"\t·\t"+total_days+"天\t·\t"+photo_number+"图";//最终传递时间
                final String days = travelRecommendFirst.getLog().getStart_date();
                List<TravelRecommendFirst.LogBean.PostsBean> posts = travelRecommendFirst.getLog().getPosts();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        imageLoader.displayImage(cover, imageView2);
                        imageLoader.displayImage(avatar, imageView3);

                        textView1.setText(nickname);
                        textView2.setText("来源:"+source);
                        textView3.setText(name);
                        textView4.setText(days);
                    }
                });

                //传递的数组图片集合
                items.addAll(posts);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });

    }

    class ViewHold extends BaseViewHolder {
        @BindView(R.id.iv_travel_recommend_first_adapter)
        ImageView imageView;

        @BindView(R.id.iv_travel_recommend_first_adapter1)
        ImageView imageView1;

        @BindView(R.id.iv_travel_recommend_first_adapter2)
        ImageView imageView2;

        @BindView(R.id.iv_travel_recommend_first_adapter3)
        ImageView imageView3;

        @BindView(R.id.tv_travel_recommend_first_adapter)
        TextView textView1;
        @BindView(R.id.tv_travel_recommend_first_adapter1)
        TextView textView2;
        @BindView(R.id.tv_travel_recommend_first_adapter2)
        TextView textView3;

        @BindView(R.id.tv_travel_recommend_first_adapter3)
        TextView textView4;
        @BindView(R.id.tv_travel_recommend_first_adapter4)
        TextView textView5;
    }

    /**
     * 得到用户数据
     * @param view
     */
    public void getUser(View view) {
        Log.d("--->", "得到用户");
        //TODO 用户见面跳转
    }

    /**
     * 退出键
     * @param view
     */
    public void onBack(View view) {
        this.finish();
    }
}
